/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectRiskFeedbackService
* 项目风险反馈
*
* 修改纪录
* 2022-08-16 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2022-08-16
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectRiskEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectRiskFeedbackService;
import com.topscomm.mpm.service.impl.auto.MpmProjectRiskFeedbackServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.pojo.MpmProjectRiskFeedbackEntity;

@Service(value = "mpmProjectRiskFeedbackService")
@Scope(value = "singleton")
public class MpmProjectRiskFeedbackService extends MpmProjectRiskFeedbackServiceAuto implements IMpmProjectRiskFeedbackService {
	@Autowired
	private MpmProjectRiskService mpmProjectRiskService;
	/**
	 * @author: houyuhang
	 * @date: 2022-08-16
	 * @param entity
	 * @throws ServiceException
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(MpmProjectRiskFeedbackEntity.FieldDocno, docNo);
		}
	}
	@Override
	protected void afterApprove(TapEntity billEntity, CboApprovalNodeInsEntity nodeIns, CboApprovalNodeEntity node, SessionUserBean sessionUserBean) {
		String id = ConvertUtil.convertToString(nodeIns.getSourceid());
		MpmProjectRiskFeedbackEntity mpmProjectRiskFeedbackEntity = this.queryById(id);
		if(mpmProjectRiskFeedbackEntity == null) {
			return;
		}
		String state = ConvertUtil.convertToString(mpmProjectRiskFeedbackEntity.getState());
		String riskstatus = ConvertUtil.convertToString(mpmProjectRiskFeedbackEntity.getRiskstatus());
		String riskid = ConvertUtil.convertToString(mpmProjectRiskFeedbackEntity.getRiskid());
		// 如果审批通过且反馈的风险状态为关闭，则将风险状态置为关闭，该风险闭环
		if("9".equals(state) && MpmSystemConst.RiskStatus.RiskStatusClose.equals(riskstatus)) {
			MpmProjectRiskEntity mpmProjectRiskEntity = mpmProjectRiskService.queryById(riskid);
			if (ObjectUtil.isEmpty(mpmProjectRiskEntity)) {
				throw new ServiceException("未找到风险反馈对应的风险单据，请联系管理员");
			}
			mpmProjectRiskEntity.setRiskstatus(MpmSystemConst.RiskStatus.RiskStatusClose);
			mpmProjectRiskService.updateEntity(mpmProjectRiskEntity);
		}
		super.afterApprove(billEntity, nodeIns, node, sessionUserBean);
	}
}
